<!--
 * @FilePath     : /vision/src/components/Seller.vue
 * @Author       : 哒哒哒
 * @Date         : 2024-05-23 22:45:58
 * @Description  :  商家销售统计（横向柱状图）
 * Copyright    : G AUTOMOBILE RESEARCH INSTITUTE CO.,LTD Copyright (c) 2024.
-->
<template>
    <div class="seller com-page" :class="{ 'fullscreen': isFullScreen }">
        <div class="com-container">
            <div class="com-chart" ref="seller_ref"></div>
        </div>
        <!-- 右上角方法按钮   -->
        <FullSCreenIcon :isFullScreen="isFullScreen" @toggleFullScreen="changeFullScreen"></FullSCreenIcon>
    </div>
</template>

<script>
import { throttle } from '@/utils';
import FullSCreenIcon from './FullScreenIcon/index.vue'
import FullscreenMixin from './mixins/FullscreenMixin'
import ThemeMixins from '@/mixins/ThemeMixins';
export default {
    /* eslint-disable */
    name: 'Seller',
    mixins: [FullscreenMixin, ThemeMixins],
    components: {
        FullSCreenIcon
    },
    data() {
        return {
            // 图标实例
            chartInstance: null,
            // 服务器返回的所有数据
            allData: null,
            // 当前显示的页数
            currentPage: 1,
            // 一共的页数
            totalPage: 0,
            // 每页显示的条数
            pageSize: 5,
            // 定时器标识
            timerId: null,
            // 记录监听事件，用于取消事件
            throttledAdaptScreen: null
        }
    },
    created() {
        this.$socket.registerCallback('sellerData', this.getData)
    },
    async mounted() {
        this.initChart();
        this.$socket.send({
            action: 'getData',
            socketType: 'sellerData',
            chartName: 'seller',
            value: ''
        })
        this.adaptScreen();
        this.throttledAdaptScreen = throttle(this.adaptScreen, 100)
        window.addEventListener('resize', this.throttledAdaptScreen);
    },
    destroyed() {
        this.$socket.unRegisterCallback('sellerData')
        clearInterval(this.timerId);
        window.removeEventListener('resize', this.throttledAdaptScreen)
    },
    methods: {
        // 初始化表格
        initChart() {
            this.chartInstance = this.$echarts.init(this.$refs.seller_ref, this.theme)
            const initOption = {
                title: {
                    text: '▎商家销售统计',
                    left: 20,
                    top: 20
                },
                grid: {
                    // 四个属性限制了坐标轴的大小
                    top: '20%',
                    left: '3%',
                    right: '6%',
                    bottom: '12%',
                    // 以上的是包括标签，而不只是包括坐标轴
                    containLabel: true
                },
                xAxis: {
                    type: 'value'
                },
                yAxis: {
                    type: 'category'
                },
                tooltip: {
                    trigger: 'axis',
                    axisPointer: {
                        type: 'line',
                        z: 0,
                        lineStyle: {
                            color: '#2D3443',
                            width: 33
                        }
                    }
                },
                series: [
                    {
                        type: 'bar',
                        // 设置数字以及样式
                        label: {
                            show: true,
                            position: 'right',
                            textStyle: {
                                color: 'white'
                            }
                        },
                        // 柱的宽度
                        barWidth: 33,
                        // 柱的样式
                        itemStyle: {
                            // 设置柱的圆角
                            barBorderRadius: [0, 33, 33, 0],
                            // 设置柱的颜色，渐变 从左到右（0，0）坐标到（1，0）坐标。坐标系是x轴指向右，y轴指向下
                            color: new this.$echarts.graphic.LinearGradient(0, 0, 1, 0, [
                                // 百分比为0
                                {
                                    offset: 0,
                                    color: '#5052EE'
                                },
                                {
                                    offset: 1,
                                    color: '#AB6EE5'
                                }
                            ])
                        }
                    }
                ]
            }
            this.chartInstance.setOption(initOption)
            // 绑定事件,鼠标移入时，关闭定时器
            this.chartInstance.on('mouseover', () => {
                clearInterval(this.timerId)
            })
            // 鼠标移除时，打开定时器
            this.chartInstance.on('mouseout', () => {
                this.startInterval()
            })
        },
        // 获取数据
        async getData(data) {
            console.log('this2', this, data);
            // const {data} = await this.$http.get('seller')
            // 对数据排序
            data.sort((a, b) => a.value - b.value)
            this.allData = data;
            // 计算page Math.ceil向上取整
            this.totalPage = Math.ceil(this.allData.length / this.pageSize)
            // 渲染数据
            this.updateChart()
            // 启动定时器
            this.startInterval()
        },
        // 更新表格
        updateChart() {
            const start = (this.currentPage - 1) * this.pageSize;
            const end = this.currentPage * this.pageSize;
            const showData = this.allData.slice(start, end)
            const sellerName = showData.map(item => item.name)
            const sellerValue = showData.map(item => item.value)
            const dataOption = {
                yAxis: {
                    data: sellerName
                },
                series: [
                    {
                        data: sellerValue,
                    }
                ]
            }
            this.chartInstance.setOption(dataOption)
        },
        // 启动定时器，规律分页
        startInterval() {
            if (this.timerId) clearInterval(this.timerId)
            this.timerId = setInterval(() => {
                this.currentPage = this.currentPage % this.totalPage + 1;
                this.updateChart()
            }, 3000);
        },
        // 根据屏幕尺寸，将图标适配屏幕
        adaptScreen() {
            let chartWidth = this.$refs.seller_ref.offsetWidth;
            let fontSize = (chartWidth / 100) * 3.6
            const adaptOption = {
                title: {
                    textStyle: {
                        fontSize: fontSize
                    },
                },
                tooltip: {
                    axisPointer: {
                        lineStyle: {
                            width: fontSize
                        }
                    }
                },
                series: [
                    {
                        // 柱的宽度
                        barWidth: fontSize,
                        // 柱的样式
                        itemStyle: {
                            // 设置柱的圆角
                            barBorderRadius: [0, fontSize / 2, fontSize / 2, 0],
                        }
                    }
                ]
            }
            this.chartInstance.setOption(adaptOption)
            this.chartInstance.resize();
        }
    }
}
</script>

<style scoped lang='less'></style>